/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.glorystone.ins.utils;

import java.time.LocalDate;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.glorystone.ins.dao.LogMpr;
import com.glorystone.ins.domain.Log;
import com.glorystone.ins.domain.SysUser;
import com.glorystone.ins.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;


/**
 * 字典工具类
 * @author ThinkGem
 * @version 2014-11-7
 */
@Slf4j
public class LogUtils {
	
	public static final String CACHE_MENU_NAME_PATH_MAP = "menuNamePathMap";
	
	private static LogMpr logMpr = SpringContextHolder.getBean(LogMpr.class);

	/**
	 * 保存日志
	 */
	public static void saveLog(HttpServletRequest request, String title){
		saveLog(request, null, null, title);
	}
	
	/**
	 * 保存日志
	 */
	public static void saveLog(HttpServletRequest request, Object handler, Exception ex, String title){
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		LocalDate date = LocalDate.now();
		if (user != null && user.getTid() != null){
			Log logs = new Log();
			logs.setRequestUrl(request.getRequestURI());
			if (request.getRequestURI().equals("/main")){
				logs.setStatus("登录成功！");
			}else{
				logs.setStatus("访问成功");
			}
			logs.setRemoteAddr(request.getRemoteAddr());
			logs.setMethod(request.getMethod());
			logs.setAccessTime(date);
			logs.setUsername(user.getRealName());
            log.info("log----------------"+logs);
			logMpr.insertSelective(logs);
		}
	}

	public static void saveLogs(HttpServletRequest request, SysUser user){
		LocalDate date = LocalDate.now();
		if (user != null){
			Log logs = new Log();
			logs.setRemoteAddr(request.getRemoteAddr());
			logs.setRequestUrl(request.getRequestURI());
			logs.setAccessTime(date);
			logs.setStatus("登录失败！");
			logs.setMethod(request.getMethod());
			logs.setUsername(user.getUsername());
			log.info("log----------------"+logs);
			logMpr.insertSelective(logs);
		}
	}

}
